package com.link.play.fit.ai.ui.guide

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import androidx.core.widget.addTextChangedListener
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.databinding.AcEditPlanGoalBinding
import kotlin.math.abs
import kotlin.math.roundToInt


const val TYPE_CALORIES = "type_calories"
const val TYPE_CARBS = "type_carbs"
const val TYPE_PROTEIN = "type_protein"
const val TYPE_FATS = "type_fats"

class EditPlanGoalAc : BaseActivity<AcEditPlanGoalBinding>(AcEditPlanGoalBinding::inflate) {

    var type: String = ""
    var value: Double = 0.0
    var tempValue: Double = 0.0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        type = intent.getStringExtra("type") ?: ""
        value = abs(intent.getDoubleExtra("value", 0.0))
        tempValue = value

        when (type) {
            TYPE_CALORIES -> {
                binding.tvEditTitle.text = getString(R.string.edit_calories_goal)
                binding.pb.progressBarColor = Color.parseColor("#FF17CB81")
                binding.ivIcon.setImageResource(R.mipmap.icon_calories)
                binding.editInputLayout.setHint(getString(R.string.calories))
            }

            TYPE_CARBS -> {
                binding.tvEditTitle.text = getString(R.string.edit_carbs_goal)
                binding.pb.progressBarColor = Color.parseColor("#FFF0A670")
                binding.ivIcon.setImageResource(R.mipmap.icon_carbs)
                binding.editInputLayout.setHint(getString(R.string.carbs))
            }

            TYPE_PROTEIN -> {
                binding.tvEditTitle.text = getString(R.string.edit_protein_goal)
                binding.pb.progressBarColor = Color.parseColor("#FFED7373")
                binding.ivIcon.setImageResource(R.mipmap.icon_protein)
                binding.editInputLayout.setHint(getString(R.string.protein))
            }

            TYPE_FATS -> {
                binding.tvEditTitle.text = getString(R.string.edit_fats_goal)
                binding.pb.progressBarColor = Color.parseColor("#FF70BBF0")
                binding.ivIcon.setImageResource(R.mipmap.icon_fats)
                binding.editInputLayout.setHint(getString(R.string.fats))
            }
        }

        binding.tvNumber.text = "${tempValue.roundToInt()}"
        binding.edit.setText("${tempValue.roundToInt()}")
        binding.edit.addTextChangedListener {
            val text = binding.edit.text
            if (text?.isNotEmpty() == true) {
                tempValue = text.toString().trim().toDouble()
                binding.tvNumber.text = "${tempValue.roundToInt()}"
            } else {
                tempValue = 0.0
                binding.tvNumber.text = "0"
            }
        }

        binding.tvRevert.setOnClickListener {
            tempValue = value
            binding.tvNumber.text = "${tempValue.roundToInt()}"
            binding.edit.setText("${tempValue.roundToInt()}")
        }
        binding.tvDone.setOnClickListener {
            Log.e(TAG, "tempValue=$tempValue")
            setResult(Activity.RESULT_OK, Intent().apply {
                putExtra("result", tempValue)
            })
            finish()
        }
        binding.flBack.setOnClickListener {
            finish()
        }
    }

}